<?php
namespace sm_shop_plugin_diy\api;
use sm_shop\controller;
use sm_shop\model\tool\imageModel;
use sm_shop\model\settingModel;
use sm_shop_plugin_diy\model\diyModel;

class index extends controller{

    private $diy_id = 0;
    public function index(){



    }

    public function single(){

        global $_GPC;
        $res = [
            'status'=>0
        ];
        $this->diy_id = $_GPC['diy_id'] ?? $this->diy_id;
        $res['diy_id'] = $this->diy_id;
        if( empty( $this->diy_id ) ){

            $res['status']=1;
            $res['description'] = '请求不合法';
            echo json_encode( $res );
            die();
        }

        $diy = diyModel::single( $this->diy_id );
        $diy_arr = json_decode( $diy['diy'], true );
        foreach( $diy_arr as $key => &$item ){

            if( strpos($item['layout'],'image.')!==false ){
                foreach( $item['cubes'] as $i=>&$cube ){
                    $width = $cube['width'];
                    $height = $cube['height'];
                    $cube['image'] = str_replace('/attachment/','', $cube['image']);
//                    $cube['image'] = imageModel::resize2( $cube['image'], $width, $height );
                    $cube['image'] =  tomedia( $cube['image'] );

                    if( $cube['page_link'] == 'product' ){

                        $cube['link'] = 'sm_shop/pages/product/product';
                        if( $cube['link_params'] ){
                            $cube['link'] += '?product_id=' + $cube['link_params']['product_id'];
                        }else{
                            $cube['link'] = 'sm_shop/pages/home/home';
                        }
                    }
                    if( $cube['page_link'] == 'home' ){
                        $cube['link'] = 'sm_shop/pages/home/home';
                    }
                    if( $cube['page_link'] == 'cart' ){
                        $cube['link'] = 'sm_shop/pages/cart/cart';
                    }
                    if( $cube['page_link'] == 'user' ){
                        $cube['link'] = 'sm_shop/pages/user/user';
                    }
                    if( $cube['page_link'] == 'order' ){
                        $cube['link'] = 'sm_shop/pages/order/list';
                    }
                    if( $cube['page_link'] == 'category' ){
                        $cube['link'] = 'sm_shop/pages/category/category';
                        $link_params = [];
                        if( !empty( $cube['link_params']['brand_id'] ) ){
                            $link_params[] = 'brand_id=' + $cube['link_params']['brand_id'];
                        }
                        if( !empty( $cube['link_params']['category_id'] ) ){
                            $link_params[] = 'category_id=' + $cube['link_params']['category_id'];
                        }
                        if( !empty( $link_params ) ){
                            $cube['link'] += '?' + explode('&', $link_params );
                        }
                    }

                }

            }



        }
//        $res['diy'] = json_decode( $diy['diy'], true );;
        echo json_encode( $diy_arr );

    }

    public function home(){

        $this->diy_id = settingModel::getItem( 'diy', 'home' );
        $this->single( );

    }


}